package twoD.hofem;

/**
 * This class forms linearCombination R->Rn of given basis functions,
 * coefficients and the dimension of range space.
 * 
 * @author Team-5
 * 
 */

public class LinearCombinationRToRn implements FunctionRToRn {

	private int n_;
	private double[] coefficients_;
	private FunctionBasisOnR basis_;

	/**
	 * Construct a LinearCombinationRToRn
	 * 
	 * @param n
	 *            The dimension of range space
	 * @param basis
	 *            Basis functions
	 */

	public LinearCombinationRToRn(int n, FunctionBasisOnR basis) {
		n_ = n;
		basis_ = basis;
	}

	/**
	 * Set the coefficients of LinearCombination
	 * 
	 * @param coefficients
	 *            The coefficients
	 * 
	 */
	public void setCoefficients(double... coefficients) {
		coefficients_ = coefficients;
	}

	public Interval getDomain() {

		return basis_.getDomain();
	}

	public int getN() {

		return n_;
	}

	public double[] tangentAt(double x) {

		double[] result = new double[n_];

		for (int i = 0; i < n_; i++) {
			result[i] = coefficients_[i]
					* basis_.getBasisFunction(0).derivativeAt(x);
			for (int j = 1; j < basis_.getDimension(); j++) {
				result[i] += coefficients_[i + j * n_]
						* basis_.getBasisFunction(j).derivativeAt(x);
			}
		}
		return result;
	}

	public double[] valueAt(double x) {

		double[] result = new double[n_];

		for (int i = 0; i < n_; i++) {
			result[i] = coefficients_[i]
					* basis_.getBasisFunction(0).valueAt(x);
			for (int j = 1; j < basis_.getDimension(); j++) {
				result[i] += coefficients_[i + j * n_]
						* basis_.getBasisFunction(j).valueAt(x);
			}
		}
		return result;
	}

}
